package P034_ConstructorAsSimpleAsPossible;

import org.junit.Test;

/**
 * @ClassName : ConstructorAsSimpleAsPossible
 * @Description : 构造函数尽量简化
 * @Author : wukj1
 * @Date: 2022/3/8 10:18
 **/
public class ConstructorAsSimpleAsPossible {

    @Test
    public void test(){
        Server server = new SimpleServer(1000);
    }

}

/**
 * 定义一个服务
 */
abstract class Server{
    public final static int DEFAULT_PORT = 40000;
    public Server(){
        //获得子类提供的端口号
        int port = getPort();
        System.out.println("端口号：" + port);
    }
    //由子类提供和端口号，并做可用性检查
    protected abstract int getPort();

    public void start(){
//        int port = getPort();
//        System.out.println("端口号：" + port);
    }
}

class SimpleServer extends Server{
    private int port = 100;
    //初始化传递一个端口号
    public SimpleServer(int _port) {
        port = _port;
        //等父类初始化完毕后，再调用
        //start();
    }

    @Override
    protected int getPort() {
        return Math.random() > 0.5 ? port : DEFAULT_PORT;
    }
}
